/*
 * Copyright 2008 Lamen Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
*/

package org.lamen.daemon;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Provides wrapper function to get daemon configuration parameters.
 *
 * @author danielfireman
 *
 * @date Jan 12, 2009
 */
public class Configurator {
  
  /**
   * Keep daemon condiguration files.
   */
  private Properties daemonProperties;

  /**
   * Loads and user specified daemon configuration file.
   * 
   * @param daemonPropertiesFileName An user-specified file name of the daemon configuration file.
   * @throws IOException If something wrong occurs while loading the daemon configuration file.
   */
  public Configurator(String daemonPropertiesFileName) throws IOException {
    this(new File(daemonPropertiesFileName));
  }
  
  /**
   * Loads and user specified daemon configuration file.
   * 
   * @param daemonPropertiesFile An user-specified file
   * @throws IOException If something wrong occurs while loading the daemon configuration file.
   */
  public Configurator(File daemonPropertiesFile) throws IOException {
    daemonProperties = new Properties();
    daemonProperties.load(new BufferedInputStream(new FileInputStream(daemonPropertiesFile)));
  }
  
  /**
   * Constructor used by tests
   * @param daemonProp A pre-defined Properties object
   */
  protected Configurator(Properties daemonProp){
    daemonProperties = daemonProp;
  }
  
  /**
   * @return The daemon listening port.
   */
  public int getPort() {
    return Integer.parseInt(daemonProperties.getProperty(Constants.PORT_PROP));
  }
}